// 166. 分数到小数
function fractionToDecimal(numerator: number, denominator: number): string {
    //!模拟手动计算的过程，在map里存着每一步的计算结果
    
    let sign = (numerator>0&&denominator>0)||(numerator<0&&denominator<0)||numerator==0
    numerator = Math.abs(numerator)
    denominator = Math.abs(denominator)
    const integer = Math.floor(numerator / denominator)
    let frac = ''
    const map:Map<number,number> = new Map()
    let i = numerator%denominator
    let index = 0
    while(!map.has(i)&&i!=0){
        map.set(i,index++)
        i*=10
        frac+=Math.floor(i/denominator)
        i%=denominator
    }
    let ans = String(integer)
    if(i==0){
        if(frac!='') ans+= `.${frac}`
    }else{
        let index = map.get(i)!
        ans += `.${frac.substring(0,index)}(${frac.substring(index)})`
    }
    return sign?ans:'-'+ans
};

class NestedInteger{}
// 341. 扁平化嵌套列表迭代器
//! 假迭代
// class NestedIterator {
//     list:number[]

//     search(item:NestedInteger){
//         if(item.isInteger()){
//             this.list.push(item.getInteger()!)
//             return
//         }
//         let l = item.getList()
//         for(let it of l){
//             this.search(it)
//         }
//     }
//     constructor(nestedList: NestedInteger[]) {
//         this.list = []
//         for(let it of nestedList){
//             this.search(it)
//         }
//     }

//     hasNext(): boolean {
//          return this.list.length != 0
//     }

// 	next(): number {
// 		return this.list.shift()!
//     }
// }
//! 真迭代
/**
 * // This is the interface that allows for creating nested lists.
 * // You should not implement it, or speculate about its implementation
 * class NestedInteger {
 *     If value is provided, then it holds a single integer
 *     Otherwise it holds an empty nested list
 *     constructor(value?: number) {
 *         ...
 *     };
 *
 *     Return true if this NestedInteger holds a single integer, rather than a nested list.
 *     isInteger(): boolean {
 *         ...
 *     };
 *
 *     Return the single integer that this NestedInteger holds, if it holds a single integer
 *     Return null if this NestedInteger holds a nested list
 *     getInteger(): number | null {
 *         ...
 *     };
 *
 *     Set this NestedInteger to hold a single integer equal to value.
 *     setInteger(value: number) {
 *         ...
 *     };
 *
 *     Set this NestedInteger to hold a nested list and adds a nested integer elem to it.
 *     add(elem: NestedInteger) {
 *         ...
 *     };
 *
 *     Return the nested list that this NestedInteger holds,
 *     or an empty list if this NestedInteger holds a single integer
 *     getList(): NestedInteger[] {
 *         ...
 *     };
 * };
 */

class NestedIterator {
    stack:NestedInteger[]
    constructor(nestedList: NestedInteger[]) {
        this.stack = new Array()
        for(let i=nestedList.length-1;i>=0;i--){
            this.stack.push(nestedList[i])
        }
    }

    hasNext(): boolean {
        //*判断有没有next的时候就把list展开进行判断
        if(this.stack.length==0) return false
        let top = this.stack.pop()!
        if(top.isInteger()) {
            this.stack.push(top)
            return true
        }
        else{
            let list = top.getList()
            for(let i=list.length-1;i>=0;i--){
                this.stack.push(list[i])
            }
            return this.hasNext()
        }
    }

	next(): number {
        //*判断hasNext()的时候已经展开了，栈顶肯定是integer，直接返回
		return this.stack.pop()!.getInteger()!
    }
}

/**
 * Your ParkingSystem object will be instantiated and called as such:
 * var obj = new NestedIterator(nestedList)
 * var a: number[] = []
 * while (obj.hasNext()) a.push(obj.next());
 */